package com.landmesure.settings;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;

import com.entrance.FragmentUpdate;
import com.landmesure.bluetoothchat.R;
import com.landmesure.bluetoothchat.view.BluetoothFragment;
import com.landmesure.settings.fragment.ArgumentsFragment;
import com.landmesure.settings.fragment.DataStatusFragment;
import com.landmesure.settings.fragment.ResultInfoFragment;


public class SettingActivity extends AppCompatActivity {
    private static final String TAG = "SettingActivity";

    private SparseArray<Fragment> mSparseArray = new SparseArray<>();
    private ArgumentsFragment mArgumentsFragment;
    private ResultInfoFragment mResultFragment;
    private boolean mIsArgsFragment = true;    //判断是否在参数fragment界面

    private FragmentUpdate mFragmentUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        mFragmentUpdate = new FragmentUpdate(this);
        mArgumentsFragment = new ArgumentsFragment();
        mResultFragment = new ResultInfoFragment();
        final FragmentUpdate.UpdateFragment mUdtFrag = mResultFragment;
        setContentView(R.layout.activity_setting);

        BluetoothFragment.landCommunicationServiceInstance.setUfInstance(mUdtFrag);
        initSparseArray();

        //显示默认属性页面
        displayFragment(R.id.main_display, mSparseArray.get(R.id.property_info));

        //导航栏选中监听
        RadioGroup navigation = findViewById(R.id.radiogroup_fragment);
        navigation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Fragment fragment = mSparseArray.get(checkedId);
                mIsArgsFragment = fragment.equals(mArgumentsFragment);
                displayFragment(R.id.main_display, fragment);
            }
        });
    }

    /**
     * 建立Fragment与对应radioButton的映射关系
     */
    private void initSparseArray() {
        mSparseArray.put(R.id.property_info, mArgumentsFragment);
        mSparseArray.put(R.id.data_status, new DataStatusFragment());
        mSparseArray.put(R.id.result_info, mResultFragment);
    }

    /**
     * 根据选项卡选中状态显示对应fragment
     * @param cId 包含显示fragment的视图容器
     * @param fragment 要显示的容器的对象
     */
    public void displayFragment(int cId, Fragment fragment) {
        mFragmentUpdate.displayFragment(cId, fragment);
    }

    /**
     *
     */
    public ArgumentsFragment getArgsFragInstance() {
        if (null == mArgumentsFragment) {
            Log.e(TAG, "getInstance, mArgumentsFragment is null");
            return null;
        }
        return mArgumentsFragment;
    }

    /**
     * 设置参数
     */
    public void onSettingClick(View v) {
        if (null == mArgumentsFragment) {
            Log.e(TAG, "onSettingClick, mArgumentsFragment is null");
            return;
        }
        mArgumentsFragment.setEntityType();
    }

    /**
     * 选测
     */
    public void onChoiceTestClick(View v) {
        if (null == mArgumentsFragment) {
            Log.e(TAG, "onChoiceTestClick, mArgumentsFragment is null");
            return;
        }
        mArgumentsFragment.choiceTest();
    }

    /**
     * 查询参数
     */
    public void onQueryArgsClick(View v) {
        if (null == mArgumentsFragment) {
            Log.e(TAG, "onQueryArgsClick, mArgumentsFragment is null");
            return;
        }
        mArgumentsFragment.queryArgs();
    }

    /**
     * 复位参数
     */
    public void onResetClick(View v) {
        if (null == mArgumentsFragment) {
            Log.e(TAG, "onResetClick, mArgumentsFragment is null");
            return;
        }

        mArgumentsFragment.reset();
    }

    /**
     * 随时刷新Fragment
     */
    public void updateFragment(Fragment fragment) {
        mFragmentUpdate.updateFragment(R.id.main_display, fragment);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public boolean getIsArgsFragment() {
        return mIsArgsFragment;
    }
}
